<?php

/**
 * genCodeValueAdmin actions.
 *
 * @package    sandbox
 * @subpackage genCodeValueAdmin
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 8507 2008-04-17 17:32:20Z fabien $
 */
class genCodeValueAdminActions extends sfActions
{
  public function executeIndex(sfWebRequest $request)
  {
    //If code set name is not defined -- redirect to the codesetAdmin module.
  	if(!$request->getParameter('code_set_nm'))
      $this->redirect('codesetAdmin/index');
      
      
  	$this->list = CMGeneralCodeValue::getCodeValuesForSet($request->getParameter('code_set_nm'), 
                    array('for_dropdown'=>'0', 'status_cd' => "('A', 'I', 'N')"));
    
    //If there are no codes returned then redirect to the create page.
    if (!isset($this->list[0]))
    {
      $this->redirect('genCodeValueAdmin/new?code_set_nm='.$request->getParameter('code_set_nm'));
    }
    else
    {
	  /*
	   * These are all for just for converting the codes to their short descriptions.
	   * Tried to do this with a Join and things weren't working as I had expected in doctrine.
	   * So this is my interim solution.
	   */
	  $this->status = CMGeneralCodeValue::getCodeValuesForSet('Code_STS', 
	                    array('for_dropdown'=>'1', 'status_cd' => "('A', 'I', 'N')"));
	  $this->auth = CMGeneralCodeValue::getCodeValuesForSet('Code_AUTH', 
	                  array('for_dropdown'=>'1', 'status_cd' => "('A', 'I', 'N')"));
	  $this->parent = CMGeneralCodeValue::getCodeValuesForSet($request->getParameter('code_set_nm'), 
	                    array('for_dropdown'=>'1', 'status_cd' => "('A', 'I', 'N')")); 
    } 

  }
  public function executeShow(sfWebRequest $request)
  {
    $this->cm_general_code_value = Doctrine::getTable('CMGeneralCodeValue')->find(array($request->getParameter('code_set_nm'),
                          $request->getParameter('code')));
    $this->forward404Unless($this->cm_general_code_value);
  }
  public function executeNew(sfWebRequest $request)
  {
    $this->form = new CMGeneralCodeValueForm();
    $this->form->setDefault('code_set_nm', $request->getParameter('code_set_nm'));

  }
  
  public function executeCreate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod('post'));

    $this->form = new CMGeneralCodeValueForm();

    $this->processForm($request, $this->form);

    $this->setTemplate('new');
  }

  public function executeEdit(sfWebRequest $request)
  {
    $this->forward404Unless($cm_general_code_value = Doctrine::getTable('CMGeneralCodeValue')->find(array($request->getParameter('code_set_nm'),
    $request->getParameter('code'))), sprintf('Object cm_general_code_value does not exist (%s).', array($request->getParameter('code_set_nm'),
    $request->getParameter('code'))));
    $this->form = new CMGeneralCodeValueForm($cm_general_code_value);
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod('post') || $request->isMethod('put'));
    $this->forward404Unless($cm_general_code_value = Doctrine::getTable('CMGeneralCodeValue')->find(array($request->getParameter('code_set_nm'),
    $request->getParameter('code'))), sprintf('Object cm_general_code_value does not exist (%s).', array($request->getParameter('code_set_nm'),
    $request->getParameter('code'))));
    $this->form = new CMGeneralCodeValueForm($cm_general_code_value);

    $this->processForm($request, $this->form);

    $this->setTemplate('edit');
  }

  public function executeDelete(sfWebRequest $request)
  {
    $request->checkCSRFProtection();

    $this->forward404Unless($cm_general_code_value = Doctrine::getTable('CMGeneralCodeValue')->find(array($request->getParameter('code_set_nm'),
    $request->getParameter('code'))), sprintf('Object cm_general_code_value does not exist (%s).', array($request->getParameter('code_set_nm'),
    $request->getParameter('code'))));
    $cm_general_code_value->delete();

    $this->redirect('genCodeValueAdmin/index?code_set_nm='.$cm_general_code_value['code_set_nm']);
  }
  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()));
    if ($form->isValid())
    {
      $cm_general_code_value = $form->save();

      $this->redirect('genCodeValueAdmin/show?code_set_nm='.$cm_general_code_value['code_set_nm'].'&code='.$cm_general_code_value['code']);
    }
  }
  private function getCMGeneralCodeValueForm($code_set_nm, $code)
  {
  	$cm_general_code_value = CMGeneralCodeValue::getCodeValue($code_set_nm, $code);
    
    if ($cm_general_code_value instanceof CMGeneralCodeValue)
    {
      return new CMGeneralCodeValueForm($cm_general_code_value);
    }
    else
    {
      return new CMGeneralCodeValueForm();
    }
  }
}
